﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Bus_proj.website
{
    public partial class Promotion_item : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Table_promotion_item x = new Table_promotion_item();
                x.promotion_item_id = Int32.Parse(TextBox1.Text);
                x.promotion_item_name = TextBox4.Text;
                x.promotion_item_distance = Int32.Parse(TextBox2.Text);
                x.promotion_item_status = false;
                db.Table_promotion_items.InsertOnSubmit(x);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
            Response.Redirect("Promotion_item.aspx");
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                if (CheckBox1.Checked == true)
                {
                    try
                    {
                        var q = from p in db.Table_promotion_items
                                where p.promotion_item_id == Int32.Parse(GridView1.Rows[i].Cells[1].Text)
                                select p;
                        q.ToList().ForEach(c => c.promotion_item_status = true);
                        db.SubmitChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                i++;
            }
            GridView1.DataBind();            
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                if (CheckBox1.Checked == true)
                {
                    try
                    {
                        var q = from p in db.Table_promotion_items
                                where p.promotion_item_id == Int32.Parse(GridView1.Rows[i].Cells[1].Text)
                                select p;
                        q.ToList().ForEach(c => c.promotion_item_status = false);
                        db.SubmitChanges();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                i++;
            }
            GridView1.DataBind();
        }
    }
}